package com.wanxi.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wanxi.dto.ResultDto;
import com.wanxi.model.MajorModel;
import com.wanxi.model.MajorTypeModel;
import com.wanxi.service.MajorService;
import com.wanxi.service.MajorTypeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.security.PublicKey;

/**
 * @author Li
 * @Date 2023/7/9 10:10
 */
@RestController
@RequestMapping("/back/major")
@Api(tags = "服务信息管理", value = "Open Api")
public class MajorController {
    @Autowired
    private MajorService majorService;
    @Autowired
    private MajorTypeService majorTypeService;

    @GetMapping("/findAll")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "查询服务信息", response = MajorModel.class, httpMethod = "GET")
    public ResultDto findAll(MajorModel majorModel) {
        Page<MajorModel> page = new Page<>(majorModel.getPage(), majorModel.getLimit());
        QueryWrapper<MajorModel> queryWrappers = query(majorModel);
        IPage<MajorModel> iPage = majorService.page(page, queryWrappers);
        for (MajorModel model : iPage.getRecords()) {
            if (model.getMajorId() != null) {
                MajorTypeModel majorTypeModel = majorTypeService.getById(model.getMajorId());
                model.setServiceName(majorTypeModel.getServiceType());
            }
        }
        return ResultDto.builder()
                .code(0)
                .data(iPage.getRecords())
                .count((int) iPage.getTotal())
                .build();
    }


    @GetMapping("/findById")
    @PreAuthorize("hasAnyAuthority('select')")//权限
    @ApiOperation(value = "根据id查询服务信息", response = MajorModel.class, httpMethod = "GET")
    public ResultDto findById(MajorModel majorModel) {
        MajorModel major = majorService.getById(majorModel.getId());
        return ResultDto.builder()
                .code(0)
                .data(major)
                .build();
    }

    @PostMapping("/addMajor")
    @PreAuthorize("hasAnyAuthority('add')")//权限
    @ApiOperation(value = "新增服务信息", response = MajorModel.class, httpMethod = "POST")
    public ResultDto addMajor(MajorModel majorModel) {
        majorService.save(majorModel);
        return ResultDto.builder()
                .code(0)
                .msg("新增成功！")
                .build();
    }

    @PostMapping("/updateMajor")
    @PreAuthorize("hasAnyAuthority('update')")//权限
    @ApiOperation(value = "修改服务信息", response = MajorModel.class, httpMethod = "POST")
    public ResultDto updateMajor(MajorModel majorModel) {
        majorService.updateById(majorModel);
        return ResultDto.builder()
                .code(0)
                .msg("修改成功！")
                .build();
    }

    @PostMapping("/deleteMajor")
    @PreAuthorize("hasAnyAuthority('delete')")//权限
    @ApiOperation(value = "删除服务信息", response = MajorModel.class, httpMethod = "POST")
    public ResultDto deleteMajor(MajorModel majorModel) {
        majorService.removeById(majorModel.getId());
        return ResultDto.builder()
                .code(0)
                .msg("删除成功！")
                .build();
    }

    private QueryWrapper<MajorModel> query(MajorModel majorModel) {
        QueryWrapper<MajorModel> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!(majorModel.getName() == null), "name", majorModel.getName());
        queryWrapper.eq(majorModel.getEnable() != null && majorModel.getEnable() != -1, "enable", majorModel.getEnable());
        return queryWrapper;
    }

}
